package com.gy.smart.view;

import android.content.Context;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.smart.MyApplication;
import com.gy.smart.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2016/7/4.
 * Describe:
 */
public class TitleView extends LinearLayout implements View.OnClickListener {

    private List<String> mDatas = new ArrayList<>();
    private List<View> items = new ArrayList<>();
    private Context mContext;
    private TitleSelectListener myListener;
    private boolean isClick = true;

    public TitleView(Context context) {
        super(context);
        mContext = context;
        this.setOrientation(LinearLayout.HORIZONTAL);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        this.setOrientation(LinearLayout.HORIZONTAL);
    }

    public void setMyListener(TitleSelectListener myListener) {
        this.myListener = myListener;
    }


    public void setmDatas(List<String> mDatas) {
        this.mDatas = mDatas;
        int lenght = 0;
        String maxStr = "";
        //获取最长的string并且要计算长度
        for (String str : mDatas) {
            if (str.length() > lenght) {
                lenght = str.length();
                maxStr = str;
            }
        }
        View item = View.inflate(mContext, R.layout.view_you_pin_top_item, null);
        TextView tvItem = (TextView) item.findViewById(R.id.view_youpin_item_tvs);
        float itemLenght = getCharWidth(tvItem, maxStr);//计算最长item的长度
        itemLenght += TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, mContext.getResources().getDisplayMetrics());
        if (itemLenght * mDatas.size() < MyApplication.screenWidth) {
            //如果所有按照最大长度来计算能够满足的话，则进行平分，因为一屏能够展示
            for (String str : mDatas) {
                addItem(str, 0);
            }
        } else {
            //如果所有item平分的话，一屏展示不了，需要滑动
            for (String str : mDatas) {
                addItem(str, 1);
            }
        }
    }

    /**
     * @param name 标签显示内容
     * @param flag 是否设置权重 0是   1否
     */
    private void addItem(String name, int flag) {
        View item = View.inflate(mContext, R.layout.view_you_pin_top_item, null);
        TextView tvItem = (TextView) item.findViewById(R.id.view_youpin_item_tvs);
        View line = item.findViewById(R.id.view_youpin_item_view);
        tvItem.setText(name);
        item.setId(items.size());
        if (items.size() == 0) {
            tvItem.setTextColor(mContext.getResources().getColor(R.color.title_red));
            line.setBackgroundColor(mContext.getResources().getColor(R.color.title_red));
        } else {
            tvItem.setTextColor(mContext.getResources().getColor(R.color.black));
            line.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }
        item.setOnClickListener(this);
        items.add(item);

        LinearLayout.LayoutParams params;
        if (flag == 0) {
            int itemWidth = MyApplication.screenWidth / mDatas.size();
            params = new LinearLayout.LayoutParams(itemWidth, LayoutParams.WRAP_CONTENT);
            this.addView(item, params);
        } else {
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            this.addView(item, params);
        }


    }

    /**
     * 计算每一个字符的宽度
     */
    public float getCharWidth(TextView textView, String str) {
        TextPaint textPaint = textView.getPaint();
        float textPaintWidth = textPaint.measureText(str);
        return textPaintWidth;
    }

    @Override
    public void onClick(View v) {
        int ids = v.getId();
        if(!isClick)
            return;
        if (myListener != null) {
            myListener.titleSelect(ids);
        }
        for (View view : items) {
            TextView tvItem = (TextView) view.findViewById(R.id.view_youpin_item_tvs);
            View line = view.findViewById(R.id.view_youpin_item_view);
            if (view.getId() == ids) {
                tvItem.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                line.setBackgroundColor(mContext.getResources().getColor(R.color.title_red));
                tvItem.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                tvItem.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                line.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                tvItem.setTypeface(Typeface.DEFAULT);
            }
        }
    }

    /** 设置标签选中状态*/
    public void setSelect(int id){
        for (View view : items) {
            TextView tvItem = (TextView) view.findViewById(R.id.view_youpin_item_tvs);
            View line = view.findViewById(R.id.view_youpin_item_view);
            if (view.getId() == id) {
                tvItem.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                line.setBackgroundColor(mContext.getResources().getColor(R.color.title_red));
                tvItem.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                tvItem.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                line.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                tvItem.setTypeface(Typeface.DEFAULT);
            }
        }
    }

    public interface TitleSelectListener {
        void titleSelect(int item);
    }

    public void setClick(boolean isClick){
        this.isClick = isClick;
    }

}

